/*
* Copyright (c)  2017, ueboot.com
* All rights reserved.
* 2017-10-12 20:48:22
*/
package com.ueboot.weishop.service.setting.impl;

import com.ueboot.core.repository.BaseRepository;
import com.ueboot.core.service.impl.BaseServiceImpl;
import com.ueboot.weishop.entity.setting.AttributeEntity;
import com.ueboot.weishop.entity.setting.CategoryEntity;
import com.ueboot.weishop.repository.setting.AttributeRepository;
import com.ueboot.weishop.service.setting.AttributeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by yangkui  on 2017-10-12 20:48:22.
 *
 * @since 2.0 by autocode
 */
@Slf4j
@Service
public class AttributeServiceImpl extends BaseServiceImpl<AttributeEntity> implements AttributeService{
    @Autowired
    private AttributeRepository attributeRepository;

    @Override
    protected BaseRepository getBaseRepository() {
         return attributeRepository;
    }

    @Override
    public Page<AttributeEntity> findByCategoryId(Pageable pageable, Long categoryId) {
        AttributeEntity entity = new AttributeEntity();
        CategoryEntity categoryEntity  = new CategoryEntity();
        categoryEntity.setId(categoryId);
        entity.setCategory(categoryEntity);
        ExampleMatcher matcher = ExampleMatcher.matching().withIgnoreNullValues()
                .withMatcher("category.id",matcher1 -> matcher1.exact());
        return attributeRepository.findAll(Example.of(entity,matcher),pageable);
    }

    @Override
    public List<AttributeEntity> findByCategoryId(Long categoryId) {
        return attributeRepository.findByCategoryId(categoryId);
    }
}
